package com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.service.impl;

import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.model.WsdMessageReminder;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.service.WsdMessageReminderService;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.dao.WsdMessageReminderMapper;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.dao.WsdMessageReminderMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import com.jxdinfo.hussar.common.tfmodel.TfModelEnum;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.dto.WsdMessageReminderSelectCondition;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.time.LocalDateTime;
import com.jxdinfo.hussar.example.messagereminder.wsdmessagereminder.vo.WsdMessageReminderPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import com.jxdinfo.hussar.support.mp.base.query.generator.superquery.SuperQueryGenerator;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import java.util.ArrayList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.jxdinfo.hussar.support.mp.base.query.dto.QueryConditionDto;
import com.jxdinfo.hussar.support.mp.base.query.dto.SuperQueryConditionDto;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 消息提醒数据模型服务实现类
 */
@Service("messagereminder.wsdmessagereminder.WsdMessageReminderServiceImpl")
@HussarDs("master")
public class WsdMessageReminderServiceImpl extends HussarServiceImpl<WsdMessageReminderMapper, WsdMessageReminder> implements WsdMessageReminderService {

    private static final Logger logger = LoggerFactory.getLogger(WsdMessageReminderServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private WsdMessageReminderMapper wsdMessageReminderMapper;

    @Override
    public ApiResponse<WsdMessageReminderPageVO> ConditionFilterPage(WsdMessageReminderSelectCondition wsdMessageReminderSelectCondition) {
        try {
            Page<WsdMessageReminder> pager = new Page<>(wsdMessageReminderSelectCondition.getCurrent(), wsdMessageReminderSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdMessageReminderSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdMessageReminderSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdMessageReminder> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdMessageReminder(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdMessageReminderSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdMessageReminderSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdMessageReminder> superQueryGenerator = new SuperQueryGenerator(WsdMessageReminder.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdMessageReminder> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdMessageReminder::getDelFlag, "1").or().isNull(WsdMessageReminder::getDelFlag)));
            WsdMessageReminderPageVO wsdMessageReminderPageVO = new WsdMessageReminderPageVO();
            wsdMessageReminderPageVO.setData(list.getRecords());
            wsdMessageReminderPageVO.setCount(list.getTotal());
            wsdMessageReminderPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdMessageReminderPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdMessageReminderPageVO> ConditionFilterPage_order_custom(WsdMessageReminderSelectCondition wsdMessageReminderSelectCondition) {
        try {
            Page<WsdMessageReminder> pager = new Page<>(wsdMessageReminderSelectCondition.getCurrent(), wsdMessageReminderSelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdMessageReminderSelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdMessageReminderSelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdMessageReminder> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdMessageReminder(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdMessageReminderSelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdMessageReminderSelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdMessageReminder> superQueryGenerator = new SuperQueryGenerator(WsdMessageReminder.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdMessageReminder> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdMessageReminder::getDelFlag, "1").or().isNull(WsdMessageReminder::getDelFlag)).and(wrapper -> wrapper.eq(WsdMessageReminder::getReminderUserId,wsdMessageReminderSelectCondition.getUserId())));
            WsdMessageReminderPageVO wsdMessageReminderPageVO = new WsdMessageReminderPageVO();
            wsdMessageReminderPageVO.setData(list.getRecords());
            wsdMessageReminderPageVO.setCount(list.getTotal());
            wsdMessageReminderPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdMessageReminderPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<Boolean> flagDelete(List<String> ids) {
        try {
            return ApiResponse.success(wsdMessageReminderMapper.flagDelete(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<Boolean> del(List<String> ids) {
        try {
            return ApiResponse.success(this.removeByIds(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdMessageReminder> formQuery(String id) {
        try {
            return ApiResponse.success(this.getById(id));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> insertOrUpdate(WsdMessageReminder wsdMessageReminder) {
        try {
            if (wsdMessageReminder.getBusinessId() == null || this.getById(wsdMessageReminder.getBusinessId()) == null) {
                wsdMessageReminder.setCreator(BaseSecurityUtil.getUser().getId());
                wsdMessageReminder.setCreateTime(LocalDateTime.now());
                wsdMessageReminder.setUpdateUserId(BaseSecurityUtil.getUser().getId());
                wsdMessageReminder.setUpdateTime(LocalDateTime.now());
                wsdMessageReminder.setDelFlag("0");
                wsdMessageReminder.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            } else {
                wsdMessageReminder.setUpdateUserId(BaseSecurityUtil.getUser().getId());
                wsdMessageReminder.setUpdateTime(LocalDateTime.now());
            }
            this.saveOrUpdate(wsdMessageReminder);
            return ApiResponse.success(String.valueOf(wsdMessageReminder.getBusinessId()), "");
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_ADD_OR_MODIFY.getMessage());
        }
    }

    @Override
    public ApiResponse<Boolean> updateStatus(List<String> ids) {
        try {
            return ApiResponse.success(wsdMessageReminderMapper.updateStatus(ids));
        } catch (Exception e) {
            throw new HussarException("操作失败");
        }
    }

    @Override
    public ApiResponse<WsdMessageReminderPageVO> queryListByUserIdOrRoleId(WsdMessageReminderSelectCondition wsdMessageReminderSelectCondition) {
        Page<WsdMessageReminder> pager = new Page<>(wsdMessageReminderSelectCondition.getCurrent(), wsdMessageReminderSelectCondition.getSize());
        String[] roleIds = wsdMessageReminderSelectCondition.getRoleId();
        String orderColumn = "";//排序的列
        String order = "";//升序/降序
        if (wsdMessageReminderSelectCondition.getOrders() != null && wsdMessageReminderSelectCondition.getOrders().size() > 0) {
            //获取页面排序的是哪一列
            OrderItem orderItem = wsdMessageReminderSelectCondition.getOrders().get(0);//获取排序的内容
            orderColumn = orderItem.getColumn();
            if (orderItem.isAsc()) {
                order = "asc";
            } else {
                order = "desc";
            }

        }
        List<SuperQueryConditionDto> children = new ArrayList<>();
        if (wsdMessageReminderSelectCondition.getSuperQueryConditionDto() != null && wsdMessageReminderSelectCondition.getSuperQueryConditionDto().size() >0) {
            //获取筛选条件列表
            children = wsdMessageReminderSelectCondition.getSuperQueryConditionDto().get(0).getChildren();
        }
        List<WsdMessageReminder> wsdMessageReminderList = wsdMessageReminderMapper.queryListByUserIdOrRoleId(pager,wsdMessageReminderSelectCondition.getUserId(), Arrays.asList(roleIds),orderColumn,order,children);
        WsdMessageReminderPageVO wsdMessageReminderPageVO = new WsdMessageReminderPageVO();
        wsdMessageReminderPageVO.setData(wsdMessageReminderList);
        wsdMessageReminderPageVO.setCount(pager.getTotal());
        wsdMessageReminderPageVO.setCode(RETURN_CODE);
        return ApiResponse.success(wsdMessageReminderPageVO);
    }
}
